# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import time
from com.zzh.utils.pagingUtils import MyPaging
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class PaymentAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def addPayment(self, payment_info):
        paymentName = payment_info['paymentName']
        paymentImgUrl = payment_info['paymentImgUrl']
        paymentRemark = payment_info['paymentRemark']
        paymentType = payment_info['paymentType']
        paymentPoundageType = payment_info['paymentPoundageType']
        paymentPoundageAmount = payment_info['paymentPoundageAmount']
        paymentSort = payment_info['paymentSort']
        paymentIsLock = payment_info['paymentIsLock']
        paymentApiPath = payment_info['paymentApiPath']

        sql = "insert into zzh_payment (payment_name,payment_img_url,payment_remark,payment_type,payment_poundage_type,payment_poundage_amount,payment_sort,payment_is_lock,payment_api_path) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [paymentName, paymentImgUrl, paymentRemark, paymentType, paymentPoundageType,
                               paymentPoundageAmount, paymentSort, paymentIsLock, paymentApiPath])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs


    def selectPayment(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_payment WHERE payment_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def updatePaymentById(self, newUpdatePayment, paymentId):
        paymentName = newUpdatePayment['paymentName']
        paymentImgUrl = newUpdatePayment['paymentImgUrl']
        paymentRemark = newUpdatePayment['paymentRemark']
        paymentType = newUpdatePayment['paymentType']
        paymentPoundageType = newUpdatePayment['paymentPoundageType']
        paymentPoundageAmount = newUpdatePayment['paymentPoundageAmount']
        paymentSort = newUpdatePayment['paymentSort']
        paymentIsLock = newUpdatePayment['paymentIsLock']
        paymentApiPath = newUpdatePayment['paymentApiPath']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_message SET payment_name=%s,payment_img_url=%s,payment_remark=%s,payment_type=%s,payment_poundage_type=%s,payment_poundage_amount=%s,payment_sort=%s,payment_is_lock=%s,payment_api_path=%s WHERE payment_id=%s"
        updatePaymentResultOfDatabase = cur.execute(sql,
                                                    [paymentName, paymentImgUrl, paymentRemark, paymentType,
                                                     paymentPoundageType, paymentPoundageAmount, paymentSort,
                                                     paymentIsLock, paymentApiPath,paymentId])
        self.conn.commit()
        if updatePaymentResultOfDatabase:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0

    def queryPaymentById(self, id):
        cur = self.conn.cursor()
        sql = "select * from zzh_payment WHERE payment_id=%s"
        total_count = cur.execute(sql, [id])
        if total_count:
            paymentList = cur.fetchall()
            paymentListJson = Table2JsonByYM.payment_list_to_json(paymentList)
            cur.close()
            self.conn.close()
            return total_count, paymentListJson
        else:
            return 0, None

    def deletePaymentById(self, paymentId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_payment WHERE payment_id=%s"
        for id in paymentId:
            deletePaymentResultOfDatabase = cur.execute(sql, [id])
        self.conn.commit()
        if deletePaymentResultOfDatabase:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0


    def queryPaymentByCondition(self, **kwargs):
        """

        """
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']

        fieldMap = requestFieldMapToDBField_YM.paymentMap  # 要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_payment WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_payment WHERE " + whereSentence
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList = MyPaging().getPage(total_count, pageIndex, pageSize, result)
            userOauthJSON =  Table2JsonByYM.payment_list_to_json(resultList)
            return userOauthJSON, total_count
        else:

            cur.close()
            self.conn.close()
            return None, 0
